home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / JActionMenuItem.java < prev    next >
Text File  |  1998-10-07  |  4KB  |  132 lines

  1. package com.symantec.itools.swing.actions;
  2.  
  3. import java.beans.*;
  4. import com.sun.java.swing.*;
  5.  
  6. public class JActionMenuItem
  7.     extends JMenuItem
  8. {
  9.     public JActionMenuItem()
  10.     {
  11.         setHorizontalTextPosition(RIGHT);
  12.         setVerticalTextPosition(CENTER);
  13.     }
  14.     
  15.     public JActionMenuItem(com.sun.java.swing.Action newAction)
  16.     {
  17.         this();
  18.         setAction(newAction);
  19.     }
  20.     
  21.     //
  22.     // Properties
  23.     //
  24.     
  25.     // Action
  26.     
  27.     public com.sun.java.swing.Action getAction()
  28.     {
  29.         return m_Action;
  30.     }
  31.     
  32.     public void setAction(com.sun.java.swing.Action newAction)
  33.     {
  34.         //Handle bad arg
  35.         if (newAction == null)
  36.             throw new IllegalArgumentException();
  37.         
  38.         //Do nothing if no change
  39.         if (newAction == m_Action)
  40.             return;
  41.         
  42.         //Release last action
  43.         if (m_Action != null)
  44.         {
  45.             removeActionListener(m_Action);
  46.             m_Action.removePropertyChangeListener(m_ActionPropertyChangeListener);
  47.         }
  48.         
  49.         m_Action = newAction;
  50.         
  51.         addActionListener(m_Action);
  52.         
  53.         m_ActionPropertyChangeListener = createActionChangeListener();
  54.         m_Action.addPropertyChangeListener(m_ActionPropertyChangeListener);
  55.         
  56.         updateMenuItem();
  57.     }
  58.     
  59.     //
  60.     // Implementation
  61.     //
  62.     
  63.     protected PropertyChangeListener createActionChangeListener()
  64.     {
  65.         return new ActionChangedListener();
  66.     }
  67.     
  68.     protected class ActionChangedListener implements PropertyChangeListener
  69.     {
  70.         public void propertyChange(PropertyChangeEvent e) {
  71.             String propertyName = e.getPropertyName();
  72.             if (propertyName.equals(Action.NAME)) {
  73.                 String text = (String) e.getNewValue();
  74.                 setText(text);
  75.             } else if (propertyName.equals("enabled")) {
  76.                 Boolean enabledState = (Boolean) e.getNewValue();
  77.                 setEnabled(enabledState.booleanValue());
  78.             } else if (propertyName.equals(Action.SMALL_ICON)) {
  79.                 Icon icon = (Icon) e.getNewValue();
  80.                 setIcon(icon);
  81.                 invalidate();
  82.                 repaint();
  83.             } 
  84.         }
  85.     }
  86.     
  87.     public void setIcon(Icon newIcon)
  88.     {
  89.         if (newIcon == null)
  90.         {
  91.             super.setIcon(null);
  92.             setDisabledIcon(null);
  93.         }
  94.         else
  95.         {
  96.             super.setIcon(newIcon);
  97.             if (!(newIcon instanceof com.sun.java.swing.ImageIcon))
  98.             {
  99.                 if (newIcon instanceof com.symantec.itools.swing.icons.ImageIcon)
  100.                 {
  101.                     setDisabledIcon
  102.                         (new com.sun.java.swing.ImageIcon
  103.                             (GrayFilter.createDisabledImage
  104.                                 (((com.symantec.itools.swing.icons.ImageIcon)newIcon).getImage())));
  105.                 }
  106.             }
  107.         }
  108.     }
  109.     
  110.     protected void updateMenuItem()
  111.     {
  112.         if (m_Action == null)
  113.         {
  114.             setText("");
  115.             setIcon(null);
  116.             setEnabled(true);
  117.         }
  118.         else
  119.         {
  120.             setText((String)m_Action.getValue(Action.NAME));
  121.             setEnabled(m_Action.isEnabled());
  122.             setIcon((Icon)m_Action.getValue(Action.SMALL_ICON));
  123.         }
  124.         
  125.         invalidate();
  126.         repaint();
  127.     }
  128.     
  129.     protected com.sun.java.swing.Action m_Action = null;
  130.     protected PropertyChangeListener    m_ActionPropertyChangeListener = null;
  131. }
  132.